/**
  ******************************************************************************
  * @file    main.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   本样例演示 GTIM1 的 ETR 门控功能
  *          当GTIM1_ETR(PB02)管脚为高电平时进行计数，为低电平时保持当前电平
  *          GTIM1_ETR(PB02) <==> H 时，GTIM1_CH4(PA03) 输出1KHz方波
  *          GTIM1_ETR(PB02) <==> L 时，GTIM1_CH4(PA03) 输出当前电平状态
  *
  * @note    硬件平台：SYM32L010_StartKit_V1.0
  *          接线说明：断开J13、J27
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "main.h"
#include "sym32l010_hal_sysctrl.h"
#include "sym32l010_hal_systick.h"
#include "sym32l010_hal_gpio.h"
#include "sym32l010_hal_gtim.h"
#include "sym32l010_Startkit_board.h"


/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Global variable definitions (declared in header file with 'extern')        */
/******************************************************************************/
GTIM_HandleTypeDef hgtim1;

/******************************************************************************/
/* Local type definitions ('typedef')                                         */
/******************************************************************************/

/******************************************************************************/
/* Local function prototypes ('static')                                       */
/******************************************************************************/
static void GTIM1_Init(void);

/******************************************************************************/
/* Local variable definitions ('static')                                      */
/******************************************************************************/

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Function implementation - global ('extern') and local ('static')           */
/******************************************************************************/


/*******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 **
 ******************************************************************************/
int32_t main(void)
{
    // 初始化系统时钟
    HAL_SysCtrl_ConfigWaitCycle(48000000);
    HAL_SysCtrl_HSI_SetDiv(HSIOSC_TO_HSI48MHZ);

    // 初始化GTIM1
    GTIM1_Init();

    // 通过杜邦线设置 GTIM1_ETR(PB02) 为高，则 GTIM1 正常计数，GTIM1_CH4(PA03) 的频率为1KHz。
    // 通过杜邦线设置 GTIM1_ETR(PB02) 为低，则 GTIM1 暂停计数，GTIM1_CH4(PA03) 保持当前电平。
    while (1)
    {
        ;
    }
}


/*******************************************************************************
 * @brief  GTIM1初始化
 * @param  None
 * @retval None
 */
static void GTIM1_Init(void)
{
    GPIO_InitTypeDef             GPIO_InitStruct;
    GTIM_SlaveConfigTypeDef      GTIM_SlaveCfgStruct;
    MemClr(&GPIO_InitStruct,     sizeof(GPIO_InitStruct));
    MemClr(&GTIM_SlaveCfgStruct, sizeof(GTIM_SlaveCfgStruct));

    HAL_SYSCTRL_GPIOA_CLK_ENABLE();
    HAL_SYSCTRL_GPIOB_CLK_ENABLE();
    HAL_SYSCTRL_GTIM1_CLK_ENABLE();

    //------------------------------------------------------------
    // 配置PA03端口为GTIM1_CH4 的输出
    AFx_GTIM1CH4_PA03();
    GPIO_InitStruct.Pins = GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(SYM_GPIOA, &GPIO_InitStruct);

    // 配置PB02端口为GTIM1_ETR输入
    AFx_GTIM1ETR_PB02();
    GPIO_InitStruct.Pins = GPIO_PIN_2;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_PULL_UP;
    HAL_GPIO_Init(SYM_GPIOB, &GPIO_InitStruct);

    //------------------------------------------------------------
    MemClr(&hgtim1, sizeof(hgtim1));

    // GTIM1时基初始化
    hgtim1.Instance               = SYM_GTIM1;
    hgtim1.Init.ClockDivision     = GTIM_CLOCKDIVISION_DIV1;
    hgtim1.Init.CounterMode       = GTIM_COUNTERMODE_UP;
    hgtim1.Init.Prescaler         = 1 - 1;                            // 不分频
    hgtim1.Init.Period            = 48000 - 1;                        // 溢出频率为1KHz (48MHz / 48000 = 1KHz)
    hgtim1.Init.AutoReloadPreload = GTIM_AUTORELOAD_PRELOAD_DISABLE;
    HAL_GTIM_Base_Init(&hgtim1);

    // GTIM1输出比较通道初始化
    hgtim1.OcInit.OCMode       = GTIM_OCMODE_PWM2;
    hgtim1.OcInit.Pulse        = 30000 - 1;
    hgtim1.OcInit.OCPolarity   = GTIM_OCPOLARITY_HIGH;
    hgtim1.OcInit.OCFastMode   = GTIM_OCFAST_DISABLE;
    HAL_GTIM_OC_ConfigChannel(&hgtim1, &hgtim1.OcInit, GTIM_CHANNEL_4);

    // GTIM1从模式配置
    GTIM_SlaveCfgStruct.SlaveMode        = GTIM_SLAVEMODE_GATED;      // 门控模式
    GTIM_SlaveCfgStruct.InputTrigger     = GTIM_TS_ETRF;              // 门控来源设置为ETRF
    GTIM_SlaveCfgStruct.TriggerFilter    = 2;                         // 滤波fSAMPLING=fDTS/1， N=4
    GTIM_SlaveCfgStruct.TriggerPolarity  = GTIM_TRIGGERPOLARITY_NONINVERTED;
    GTIM_SlaveCfgStruct.TriggerPrescaler = GTIM_TRIGGERPRESCALER_DIV1; // 不分频
    HAL_GTIM_SlaveConfigSynchro(&hgtim1, &GTIM_SlaveCfgStruct);

    // 启动GTIM1的CH4输出
    HAL_GTIM_OC_Start(&hgtim1, GTIM_CHANNEL_4);
}


/*******************************************************************************
 ** EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END */
}
#endif /* USE_FULL_ASSERT */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
